﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace 玩具商城管理系统.Tproduct
{
    public partial class Treduce : Form
    {
        public Treduce()
        {
            InitializeComponent();
        }
        SqlDataAdapter daProudct, dalog;
        DataSet ds = new DataSet();
        void init()
        {
            DB.GetCn();
            string str = "select * from commodity";
            string sdr = "select * from log";
            daProudct = new SqlDataAdapter(str, DB.cn);
            dalog = new SqlDataAdapter(sdr, DB.cn);
            daProudct.Fill(ds, "product_info");
            dalog.Fill(ds, "log_info");
        }
        void showAll()
        {
            DataView dvProduct = new DataView(ds.Tables["product_info"]);
            dgvProduct.DataSource = dvProduct;
        }
        private void Treduce_Load(object sender, EventArgs e)
        {
            showXz();
            init();
            showAll();
        }

        private void dgvProduct_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if(dgvProduct.Rows.Count > 0) 
            { 
                for (int i=0; i<dgvProduct.Rows.Count;i++)
                {
                    DataGridViewCheckBoxCell ck = dgvProduct.Rows[i].Cells["acCode"] as DataGridViewCheckBoxCell;
                    if(i!=e.RowIndex)
                    {
                        ck.Value = false;
                    }
                    else
                    {
                        ck.Value = true;
                    }
                }
               

            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int s =dgvProduct.Rows.Count;
            for (int i=0; i<dgvProduct.Rows.Count;i++)
            {
                if (dgvProduct.Rows[i].Cells["acCode"].EditedFormattedValue.ToString() == "True")
                {
                    DialogResult dr = MessageBox.Show("您确定要对此商品进行删除吗", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                    if (dr == DialogResult.OK)
                    {
                        dgvProduct.Rows.RemoveAt(i);
                        //添加日志
                        DataRow drlog = ds.Tables["log_info"].NewRow();
                        drlog["username"] = login.username;
                        drlog["type"] = "删除";
                        drlog["action_date"] = DateTime.Now;
                        drlog["action_table"] = "commodity表";
                        ds.Tables["log_info"].Rows.Add(drlog);

                        UpdateDB();
                    }
                    else
                    {
                        return;
                    }
                }
                else
                {
                    s = s - 1;

                }
            }
            if(s==0)
            {
                MessageBox.Show("请选择你要删除的商品");
            }
        }
        void UpdateDB()
        {
            try
            {
                SqlCommandBuilder dbProduct = new SqlCommandBuilder(daProudct);
                daProudct.Update(ds, "product_info");
                SqlCommandBuilder dblog = new SqlCommandBuilder(dalog);
                dalog.Update(ds, "log_info");
            }
            catch(SqlException ex)
            {
                MessageBox.Show(ex.Message.ToString());
            }
            DB.cn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        void showXz()
        {
            DataGridViewCheckBoxColumn acCode =new DataGridViewCheckBoxColumn();
            acCode.Name = "acCode";
            acCode.HeaderText = "选择";
            dgvProduct.Columns.Add(acCode);

        }

    }
}
